home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Data 2002 May / CD Rom Data Mayıs 2002.iso / Freeware / Blitz Basic / data1.cab / Support / help / beginners / tutorial / deepend / 5 - net example uncommented only.bb < prev    next >
Encoding:
Text File  |  2002-04-10  |  6.9 KB  |  197 lines

  1.  
  2. ; NOTE: Not commented, as it's quite advanced, but it demonstrates what's
  3. ; possible!
  4.  
  5. ; -----------------------------------------------------------------------------
  6. ; LoadWebImage -- uses BlitzGet Deluxe, based on Mark Sibly's HTTPGet
  7. ; -----------------------------------------------------------------------------
  8. ; james@hi-toro.com
  9. ; -----------------------------------------------------------------------------
  10.  
  11. AppTitle "LoadWebImage"
  12.  
  13. Graphics 640, 480
  14.  
  15. SetBuffer BackBuffer ()
  16.  
  17. ; -----------------------------------------------------------------------------
  18. ; Load an image from the web, straight into our game!
  19. ; -----------------------------------------------------------------------------
  20. rocket = LoadWebImage ("http://www.hi-toro.com/images/rocket.bmp")
  21.  
  22. ; -----------------------------------------------------------------------------
  23. ; Check for failure
  24. ; -----------------------------------------------------------------------------
  25. If rocket = 0
  26.  
  27.     ; RuntimeError "Failed to load web image!": End
  28.     
  29.     ; Alternative (BETTER) failure method -- use a default local image supplied with your game...
  30.  
  31.     rocket = LoadImage ("rocket.bmp")
  32.     
  33. EndIf
  34.  
  35. MaskImage rocket, 255, 0, 255
  36.  
  37. x = 50
  38. y = 50
  39.  
  40. ClsColor 70, 110, 190
  41.  
  42. Repeat
  43.  
  44.     Cls
  45.         
  46.     If KeyDown (203) = 1 Then x = x - 1
  47.     If KeyDown (205) = 1 Then x = x + 1
  48.     If KeyDown (200) = 1 Then y = y - 1
  49.     If KeyDown (208) = 1 Then y = y + 1
  50.  
  51.     DrawImage rocket, x, y
  52.  
  53.     Flip
  54.  
  55. Until KeyDown (1) = 1
  56.  
  57. End
  58.  
  59. Function LoadWebImage (webFile$)
  60.     If BlitzGet (webFile$, CurrentDir (), "temp_web_image.bmp")
  61.         image = LoadImage ("temp_web_image.bmp")
  62.         DeleteFile "temp_web_image.bmp"
  63.     EndIf
  64.     Return image
  65. End Function
  66.  
  67. Function BlitzGet (webFile$, saveDir$, saveFile$)
  68.  
  69.     ; -------------------------------------------------------------------------
  70.     ; Strip "http://" if provided
  71.     ; -------------------------------------------------------------------------
  72.     If Left (webFile$, 7) = "http://" Then webFile$ = Right (webFile$, Len (webFile$) - 7)
  73.  
  74.     ; -------------------------------------------------------------------------
  75.     ; Split into hostname and path/filename to download
  76.     ; -------------------------------------------------------------------------
  77.     slash = Instr (webFile$, "/")
  78.     If slash
  79.         webHost$ = Left (webFile$, slash - 1)
  80.         webFile$ = Right (webFile$, Len (webFile$) - slash + 1)
  81.     Else
  82.         webHost$ = webFile$
  83.         webFile$ = "/"
  84.     EndIf
  85.         
  86.     ; -------------------------------------------------------------------------
  87.     ; Add trailing slash to download dir if not given
  88.     ; -------------------------------------------------------------------------
  89.     If Right (saveDir$, 1) <> "\" Then saveDir$ = saveDir$ + "\"
  90.  
  91.     ; -------------------------------------------------------------------------
  92.     ; Save filename -- get from webFile$ if not provided
  93.     ; -------------------------------------------------------------------------
  94.     If saveFile$ = ""
  95.         If webFile = "/"
  96.             saveFile$ = "Unknown file.txt"
  97.         Else
  98.             For findSlash = Len (webFile$) To 1 Step - 1
  99.                 testForSlash$ = Mid (webFile$, findSlash, 1)
  100.                 If testForSlash$ = "/"
  101.                     saveFile$ = Right (webFile$, Len (webFile$) - findSlash)
  102.                     Exit
  103.                 EndIf
  104.             Next
  105.             If saveFile$ = "" Then saveFile$ = "Unknown file.txt"
  106.         EndIf
  107.     EndIf
  108.  
  109.     ; DEBUG
  110.     ; RuntimeError "Web host: " + webHost$ + Chr (10) + "Web file: " + webFile$ + Chr (10) + "Save dir: " + saveDir$ + Chr (10) + "Save file: " + saveFile$
  111.  
  112.     www = OpenTCPStream (webHost$, 80)
  113.  
  114.     If www
  115.     
  116.         WriteLine www, "GET " + webFile$ + " HTTP/1.1" ; GET / gets default page...
  117.         WriteLine www, "Host: " + webHost$
  118.         WriteLine www, "User-Agent: BlitzGet Deluxe"
  119.         WriteLine www, "Accept: */*"
  120.         WriteLine www, ""
  121.         
  122.         ; ---------------------------------------------------------------------
  123.         ; Find blank line after header data, where the action begins...
  124.         ; ---------------------------------------------------------------------
  125.                 
  126.         Repeat
  127.             header$ = ReadLine (www)
  128.             If Left (header$, 16) = "Content-Length: "    ; Number of bytes to read
  129.                 bytesToRead = Right (header$, Len (header$) - 16)
  130.             EndIf
  131.         Until header$ = "" Or (Eof (www))
  132.         
  133.         If bytesToRead = 0 Then Goto skipDownLoad
  134.         
  135.         ; ---------------------------------------------------------------------
  136.         ; Create new file to write downloaded bytes into
  137.         ; ---------------------------------------------------------------------
  138.         save = WriteFile (saveDir$ + saveFile$)
  139.         If Not save Then Goto skipDownload
  140.  
  141.         ; ---------------------------------------------------------------------
  142.         ; Incredibly complex download-to-file routine...
  143.         ; ---------------------------------------------------------------------
  144.  
  145.         For readWebFile = 1 To bytesToRead
  146.         
  147.             If Not Eof (www) Then WriteByte save, ReadByte (www)
  148.             
  149.             ; Call BytesReceived with position and size every 100 bytes (slows down a LOT with smaller updates)
  150.             
  151.             tReadWebFile = readWebFile
  152.             If tReadWebFile Mod 100 = 0 Then BytesReceived (readWebFile, bytesToRead)
  153.  
  154.         Next
  155.  
  156.         CloseFile save
  157.         
  158.         ; Fully downloaded?
  159.         If (readWebFile - 1) = bytesToRead
  160.             success = 1
  161.         EndIf
  162.         
  163.         ; Final update (so it's not rounded to nearest 100 bytes!)
  164.         BytesReceived (bytesToRead, bytesToRead)
  165.         
  166.         .skipDownload
  167.         CloseTCPStream www
  168.         
  169.     Else
  170.     
  171.         RuntimeError "Failed to connect"
  172.         
  173.     EndIf
  174.     
  175.     Return success
  176.     
  177. End Function
  178.  
  179. ; -----------------------------------------------------------------------------
  180. ; User-defined update function, called every 100 bytes of download -- alter to suit!
  181. ; -----------------------------------------------------------------------------
  182. ; TIP: Pass a user-defined type instead, with all data (this stuff plus URL, local filename, etc)
  183. ; -----------------------------------------------------------------------------
  184. Function BytesReceived (posByte, totalBytes)
  185.     ; Example update code...
  186.     Cls
  187.     Text 20, 20, "Downloading file -- please wait..."
  188.     Text 20, 40, "Received: " + posByte + "/" + totalBytes + " bytes (" + Percent (posByte, totalBytes) + "%)"
  189.     Flip
  190. End Function
  191.  
  192. ; -----------------------------------------------------------------------------
  193. ; Handy percentage function
  194. ; -----------------------------------------------------------------------------
  195. Function Percent (part#, total#)
  196.     Return Int (100 * (part / total))
  197. End Function